package com.lcx.security.main;



import com.lcx.security.FFmpegCommandManager.FFmpegManager;
import com.lcx.security.FFmpegCommandManager.FFmpegManagerImpl;
import com.lcx.security.config.StreamConfig;
import com.lcx.security.myenum.StreamEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 测试
 * @since jdk1.7
 */
public class FFmpegCMD {
	protected static Logger logger = LoggerFactory.getLogger(FFmpegCMD.class);

	/**
	 * hls -> mp4
	 *
	 * @throws InterruptedException
	 */
	public static void rtspToMP4(String filePath) throws InterruptedException {

		FFmpegManager manager = new FFmpegManagerImpl();
		try {
			//-vcodec copy -acodec copy -absf aac_adtstoasc
			String hlsToMP4 = "ffmpeg -rtsp_transport tcp -re -i " + StreamConfig.URL + "  -c copy -t 3590 -f mp4 " + filePath +" -y";
			if (StreamConfig.FFMPEG_PATH!=null&&!StreamConfig.FFMPEG_PATH.trim().equals("")){
				hlsToMP4 = StreamConfig.FFMPEG_PATH+hlsToMP4;
			}
			logger.info("准备执行："+hlsToMP4);
			String start = manager.start(StreamEnum.RTSP_TO_MP4, hlsToMP4);
			logger.info("当前执行的推流任务的ID是："+start);

		} catch (Exception e) {
			e.printStackTrace();
			FFmpegCMD.stopRtspToMp4();
			manager.stopAll();
		}
	}

	public static void stopRtspToMp4() {
		Process p = null;
		try {
			String[] cmds = {"/bin/sh","-c"," ps -ef|grep 1.mp4|awk '{print $2}'|xargs kill -9"};//linux
			p = Runtime.getRuntime().exec(cmds);//linux
			logger.info("执行了："+cmds[2]);
			p.waitFor(5, TimeUnit.SECONDS);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}finally {
			if (p != null) {
				p.destroy();
			}
		}
	}


	public static void main(String[] args) throws InterruptedException {

		Calendar instance = Calendar.getInstance();
		int hour = instance.get(Calendar.HOUR_OF_DAY);

		Date date = new Date(System.currentTimeMillis() - 7 * 24 * 3600 * 1000);
		instance.setTime(date);
		int day = instance.get(Calendar.DAY_OF_MONTH);
		String fileName = StreamConfig.FILE_PATH+hour+ day +":"+".mp4";


		System.out.println(hour);
		System.out.println(day);
	}
}
